package com.visionblue.business.ship.page.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.visionblue.business.ship.R
import com.visionblue.business.ship.util.DeviceUtil
import kotlinx.android.synthetic.main.dialog_selection.*

/**
 * @author: xlh
 * @date: 2020-05-29 11:16 AM
 * @desc:
 */
class SelectionDialog(context: Context, items: List<SelectionItem>) :
    Dialog(context, R.style.blurry_slide_dialog) {

    init {
        setContentView(R.layout.dialog_selection)

        items.withIndex().forEach {
            val selectionItem = it.value
            val itemView = LayoutInflater.from(context)
                .inflate(selectionItem.layoutId, contentView, false) as TextView
            itemView.text = selectionItem.text

            if (selectionItem.action != null) {
                itemView.setOnClickListener { view ->
                    dismiss()
                    selectionItem.action.invoke(view)
                }
            }

            contentView.addView(itemView)

            val params = itemView.layoutParams as LinearLayout.LayoutParams
            if (it.index < items.size - 1) {
                params.bottomMargin = DeviceUtil.dip2px(context, 30f)
            }
            itemView.layoutParams = params
        }

        contentView.setOnClickListener {
            dismiss()
        }
    }
}

class SelectionItem(
    val text: String,
    val action: ((View) -> Unit)? = null,
    val layoutId: Int = R.layout.btn_selection_dialog
)